package com.bayihouse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.Community;
import com.bayihouse.model.request.community.CommunityAddRequest;
import com.bayihouse.model.request.community.CommunityQueryRequest;
import com.bayihouse.model.request.community.CommunityUpdateRequest;
import com.bayihouse.model.vo.CommunityVO;
import com.bayihouse.service.CommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/community")
@Slf4j
public class CommunityController {

    @Resource
    private CommunityService communityService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param communityAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Long> addCommunity(@RequestBody CommunityAddRequest communityAddRequest, HttpServletRequest request) {
        if (communityAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Community community = new Community();
        BeanUtils.copyProperties(communityAddRequest, community);
        boolean result = communityService.save(community);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(community.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteCommunity(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = communityService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param communityUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateCommunity(@RequestBody CommunityUpdateRequest communityUpdateRequest,
                                                 HttpServletRequest request) {
        System.out.println(communityUpdateRequest);
        if (communityUpdateRequest == null || communityUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Community community = new Community();
        BeanUtils.copyProperties(communityUpdateRequest, community);
        boolean result = communityService.updateById(community);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<CommunityVO> getCommunityById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Community community = communityService.getById(id);
        ThrowUtils.throwIf(community == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(CommunityVO.objToVo(community));
    }


    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<CommunityVO> getCommunityVOById(long id, HttpServletRequest request) {
        Community response = communityService.getById(id);
        return ResultUtils.success(communityService.getCommunityVO(response));
    }

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param communityQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<Community>> listCommunityByPage(@RequestBody CommunityQueryRequest communityQueryRequest,
                                                             HttpServletRequest request) {
        long current = communityQueryRequest.getCurrent();
        long size = communityQueryRequest.getPageSize();
        Page<Community> communityPage = communityService.page(new Page<>(current, size),
                communityService.getQueryWrapper(communityQueryRequest));
        return ResultUtils.success(communityPage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param communityQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<CommunityVO>> listCommunityVOByPage(@RequestBody CommunityQueryRequest communityQueryRequest,
                                                                 HttpServletRequest request) {
        if (communityQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = communityQueryRequest.getCurrent();
        long size = communityQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Community> communityPage = communityService.page(new Page<>(current, size),
                communityService.getQueryWrapper(communityQueryRequest));
        System.out.println(communityPage.getRecords());
        Page<CommunityVO> communityVOPage = new Page<>(current, size, communityPage.getTotal());
        List<CommunityVO> communityVO = communityService.getCommunityVOList(communityPage.getRecords());
        communityVOPage.setRecords(communityVO);
        System.out.println(communityVOPage.getRecords());
        return ResultUtils.success(communityVOPage);
    }


    @PostMapping("/get/all/vo")
    public BaseResponse<List<CommunityVO>> getAllCommunityVO(@RequestBody CommunityQueryRequest communityQueryRequest) {
        List<Community> communityList = communityService.list(communityService.getQueryWrapper(communityQueryRequest));
        return ResultUtils.success(communityService.getCommunityVOList(communityList));
    }

}
